package com.vinaprime.primeschedule.businesslayer;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import com.vinaprime.primeschedule.databaselayer.VPGroupLine_DL;
import com.vinaprime.primeschedule.entities.VPContact;
import com.vinaprime.primeschedule.entities.VPGroupLine;
/** * 
 * @author LONGPN
 * @since 17/06/2013
 *
 */
@SuppressLint("DefaultLocale")
public class VPGroupLine_BL {
	private VPGroupLine_DL myGroupLineDL ;
	private VPContact_BL mcontactBL ;
	
	public VPGroupLine_BL (Context context , String path){
		this.myGroupLineDL = new VPGroupLine_DL(context, path);
		this.mcontactBL = new VPContact_BL(context, path);
	}

	public int getNewGroupLineID (){
		
		int result = 0;
		Cursor cur =  myGroupLineDL.getNewGroupLineID();
		if (cur.moveToFirst())
		{
			if (!cur.isNull(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglId()))) {
				result = Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglId())));
			}
		}
		return result+1;
	}

	public VPGroupLine getGroupLinebyID (int Id) {
		VPGroupLine myGL = new VPGroupLine();
		
		Cursor cur  =  this.myGroupLineDL.getGroupLinebyId(Id);
		if(cur.moveToFirst()){
		myGL.setVpglID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglId()))));
				myGL.setVpghID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpghId()))));
				myGL.setmContact(mcontactBL.getContactbyID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpcId())))));
				myGL.setVpglDescription(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglDescription())));
				myGL.setVpglEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglEnableFlag())));
		
		}
		return myGL;		
	}
	
	public List<VPGroupLine> getGroupLineList(int headerID)
	{
		List<VPGroupLine> listGL = new ArrayList<VPGroupLine>();
		Cursor cur = myGroupLineDL.getGroupLineList(headerID);
		
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{
				VPGroupLine myGL = new VPGroupLine();
				myGL.setVpglID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglId()))));
				myGL.setVpghID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpghId()))));
				myGL.setmContact(mcontactBL.getContactbyID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpcId())))));
				myGL.setVpglDescription(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglDescription())));
				myGL.setVpglEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglEnableFlag())));
				listGL.add(myGL);
				cur.moveToNext();
				
			}
		}
		return listGL;
	
	}
	public List<VPContact> getAllContactFromGroup(int _groupID)
	{
		Cursor cur = myGroupLineDL.getGroupLineList(_groupID);
		List<VPContact> listContact = new ArrayList<VPContact>();
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{
				VPContact myContact = new VPContact();
				myContact = mcontactBL.getContactbyID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpcId()))));
				listContact.add(myContact);
				cur.moveToNext();
		}	
		}
		return listContact;
	
	}
	public List<VPGroupLine> getAllGroupLine() throws IllegalArgumentException, ParseException
	{
		List<VPGroupLine> listGL = new ArrayList<VPGroupLine>();
		
		Cursor cur  = this.myGroupLineDL.getAllGroupLine();
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{	
				
				VPGroupLine myGL = new VPGroupLine();
				myGL.setVpglID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglId()))));
				myGL.setVpghID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpghId()))));
				myGL.setmContact(mcontactBL.getContactbyID(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpcId())))));
				myGL.setVpglDescription(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglDescription())));
				myGL.setVpglEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPGroupLine_DL.getDbTableVpglEnableFlag())));
				listGL.add(myGL);
				cur.moveToNext();
			}
		}
		return listGL;
	}
	public Cursor getAllGroupLineCursor(){
		Cursor cur  =  this.myGroupLineDL.getAllGroupLine();
		return cur;
	}
	public void addGroupLine(VPGroupLine myGL){
		this.myGroupLineDL.AddGroupLine(myGL);
	}
	public void updateGroupLine(VPGroupLine myGL){
		this.myGroupLineDL.updateGroupLine(myGL);
	}
	public void deleteGroupLine(VPGroupLine myGL){
		this.myGroupLineDL.deleteGroupLine(myGL);
	}
	public void deleteGroupLinebyID(int _Vpgl_Id){
		this.myGroupLineDL.deleteGroupLinebyID(_Vpgl_Id);
	}

	
}
